﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using REVISIONMAX30.Frame;
using REVISIONMAX30.Serialization;

namespace REVISIONMAX30.DataObjects
{
    public class DataMain : DataObjectBase
    {

        public List<Group> GroupList { get; set; }
        public List<ConnectionString> ConnectionStringList { get; set; }

        public DataMain()
        {
        }

        public DataMain(bool pNew)
        {
            this.DataObjectGUID = Guid.NewGuid();
            this.Created = DateTime.Now;
            GroupList = new List<Group>();
            ConnectionStringList = new List<ConnectionString>();
        }

        public FrResultType ValidateGroupName(string pGroupName)
        {
            var tFrResult = new FrResultType();

            if (pGroupName.IsEmpty())
            {
                tFrResult.AddResult("Group Name must be specified.", FrResultSeverityType.Error);
            }
            else if (this.GroupList.Where(t => t.GroupName.Equals(pGroupName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() != null)
            {
                tFrResult.AddResult("Group Name must be unique.", FrResultSeverityType.Error);
            }

            return tFrResult;
        }

        public static DataMain Load(UIHandler.DelegateAddResult pAddResult)
        {
            DataMain dataMain = null;

            if (System.IO.File.Exists(Common.Constants.DataMainFilePath))
            {
                try
                {
                    dataMain = (DataMain)Serializer.FromXml(System.IO.File.ReadAllText(Common.Constants.DataMainFilePath), typeof(DataMain));
                }
                catch (Exception exc)
                {
                    pAddResult(new FrResultType(exc));

                    var tBackUpFileName = string.Format(Common.Constants.DataMainBackUpFilePath, Guid.NewGuid());

                    System.IO.File.Move(Common.Constants.DataMainFilePath, tBackUpFileName);

                    pAddResult(new FrResultType(string.Format("Data Main file was backed up to {0}", tBackUpFileName), FrResultSeverityType.Info));
                }
            }

            if (dataMain == null)
            {
                pAddResult(new FrResultType("Initializing new data main for storing data.", FrResultSeverityType.Info));

                dataMain = new DataMain(true);
            }

            return dataMain;
        }

        public void Save(UIHandler.DelegateAddResult pAddResult)
        {
            var tXmlData = Serializer.ToXml(this);

            try
            {
                System.IO.File.WriteAllText(Common.Constants.DataMainFilePath, tXmlData);
            }
            catch (Exception exc)
            {
                pAddResult(new FrResultType(exc));

                var tBackUpFileName = string.Format(Common.Constants.DataMainBackUpFilePath, Guid.NewGuid());

                System.IO.File.WriteAllText(Common.Constants.DataMainSaveBackUpFilePath, tXmlData);

                pAddResult(new FrResultType(string.Format("Data Main Save file was backed up to {0}.", tBackUpFileName), FrResultSeverityType.Info));
            }


        }
    }
}
